package cn.wzy.service.impl;


import cn.wzy.enums.AppHttpCodeEnum;
import cn.wzy.mapper.DepartmentMapper;
import cn.wzy.mapper.EmployeeMapper;
import cn.wzy.pojo.Department;
import cn.wzy.pojo.Employee;
import cn.wzy.results.ResponseResult;
import cn.wzy.service.DepartmentService;
import cn.wzy.vo.DepartmentVo;
import cn.wzy.vo.GoPageVo;
import cn.wzy.vo.PageVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (TDepartment)表服务实现类
 *
 * @author makejava
 * @since 2022-12-08 13:53:15
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper,Department>implements DepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private EmployeeMapper employeeMapper;
    /*
     * 获取部门
     * */
    @Override
    public ResponseResult getDepartments(Long id) {
        List<DepartmentVo> departments = departmentMapper.getDepartments(id);
        return ResponseResult.okResult(departments);
    }

    /*
    * 添加或者修改部门
    * */
    @Override
    @Transactional
    public ResponseResult saveDepartment(Department department) {
//        判断是否id存在，操作类型
        if (Objects.isNull(department.getId())){
//            添加操作
            departmentMapper.addDepartment(department);
//            获得到path
            String path = path(department);
//            修改path到数据库
            department.setPath(path);
            departmentMapper.updateDepartment(department);
            return ResponseResult.okResult();
        }else {
//            获取到path
            String path = path(department);
//            修改操作
            department.setPath(path);
            departmentMapper.updateDepartment(department);
            return ResponseResult.okResult();
        }
    }

    /*
    * 删除部门
    * */
    @Override
    @Transactional
    public ResponseResult deleteDepartment(Long id) {
//        查询该部门是否存在员工
        List<Employee> employees = employeeMapper.getEmployeeByDepartmentId(id);
        if (Objects.isNull(employees) || employees.size()<1){
//          查看是否有下级部门
            List<Department> departments = departmentMapper.getDepartmentLower(id);
            if (Objects.isNull(departments) || departments.size()<1){
//                删除部门
                departmentMapper.deleteDepartment(id);
                return ResponseResult.okResult();
            }
            else {
                return ResponseResult.errorResult(AppHttpCodeEnum.DEPARTMENT_LWER);
            }
        }
//        不可以删除
        return ResponseResult.errorResult(AppHttpCodeEnum.DEPARTMENT_DELETE);
    }

    /*
    * 分页
    * */
    @Override
    public ResponseResult getDepartmentsPage(GoPageVo goPageVo) {

//        偏移量
        Long stater = (goPageVo.getPageIndex()-1)*goPageVo.getPageSize();
//        分页查询
        List<DepartmentVo> departmentVos = departmentMapper.getDepartmentsPage(stater,goPageVo.getPageSize(),goPageVo.getKeyword());
        Long total = departmentMapper.getTotal(goPageVo);
        PageVo pageVo = new PageVo(departmentVos, total, goPageVo.getPageSize());
        return ResponseResult.okResult(pageVo);
    }

    /*
    * 批量删除部门
    * */
    @Override
    @Transactional
    public ResponseResult deleteDepartmentList(List<Long> ids) {
//        通过ids查询到所有部门
        List<Department> departmentList = departmentMapper.getDepartmentList(ids);
//        循环查到到部门，判断是否有下级级部门
        for (Department department : departmentList) {
            List<Department> departmentLower = departmentMapper.getDepartmentLower(department.getId());
            if (!Objects.isNull(departmentLower)){
//                有下级部门，不能删除
                return ResponseResult.errorResult(AppHttpCodeEnum.DEPARTMENT_LWER);
            }
//            判断是否有员工
            List<Employee> employees = employeeMapper.getEmployeeByDepartmentId(department.getId());
            if (!Objects.isNull(employees)){
                return ResponseResult.errorResult(AppHttpCodeEnum.DEPARTMENT_DELETE);
            }
        }
        return ResponseResult.okResult();
    }

    /*
    * 多级部门
    * */
    @Override
    public ResponseResult getDepLevel() {
//        自查询
        List<DepartmentVo> departmentVos = departmentMapper.getDepLevel();
        return ResponseResult.okResult(departmentVos);
    }

    /*
    *
    * 查询多级三级部门
    * */
    @Override
    public ResponseResult getDepThreeLevel() {
        List<DepartmentVo> departmentVos = departmentMapper.getDepThreeLevel();
        return ResponseResult.okResult(departmentVos);
    }


    /*
    * 获得到部门path方法
    *
    * */
    public String path(Department department){
        //            通过id修改path
//            判断是否有父id
        if (Objects.isNull(department.getParent_id()) || Objects.isNull(department)){
            String path = "/" + department.getId();
            return path;
        }else {
            //            获得到父部门
            Department departments = departmentMapper.getDepartmentById(department.getParent_id());
//            拼接path
            String path = departments.getPath() + "/" + department.getId();
            return path;
        }
    }
}
